package com.ruoyi.project.cyq.task.postpone.service.impl;

import com.ruoyi.project.cyq.task.postpone.domain.PostPone;
import com.ruoyi.project.cyq.task.postpone.domain.PostPoneVo;
import com.ruoyi.project.cyq.task.postpone.mapper.PostPoneMapper;
import com.ruoyi.project.cyq.task.postpone.service.IPostPoneService;
import com.ruoyi.project.cyq.task.task.domain.Task;
import com.ruoyi.project.cyq.task.task.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PostPoneServiceImpl implements IPostPoneService {

    @Autowired
    private PostPoneMapper postPoneMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public List<PostPoneVo> selectPostPoneList(PostPone postPone) {
        return  postPoneMapper.selectPostPoneList(postPone);
    }

    @Override
    public int insertPostPone(Task task, String taskEndTime,String postPoneCause) {
        Date postPoneTime = this.convertDate(taskEndTime);
        String taskTitle = task.getTaskTitle();
//        long userId = task.getUserId();
        Task task1 = taskMapper.selectTaskByTaskTitle(taskTitle);
        long taskId = task1.getTaskId();
        PostPone postPone = new PostPone();
        postPone.setTaskId(taskId);
        postPone.setPostPoneCause(postPoneCause);
        postPone.setPostPoneTime(postPoneTime);
        postPone.setPostPoneStatus(3);
        postPone.setCheckId(1);
        System.out.println(postPone);
        return postPoneMapper.insertpostPone(postPone);
    }

    @Override
    public List<PostPoneVo> selectPostPoneList1(PostPone postPone) {
        return  postPoneMapper.selectPostPoneList1(postPone);
    }

    public Date convertDate(String time) {
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }
}
